﻿using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Furion.LinqBuilder;
using Microsoft.EntityFrameworkCore;
using Queer.AutoJob.Core.Entity;
using Queer.AutoJob.Core.Model;
using Queer.Util;
using Queer.Util.Helper;
using Queer.Util.Model;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Queer.AutoJob.Core.Service
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2021-04-01 09:29
    /// 描 述：定时任务日志服务类
    /// </summary>

    public class AutoJobLogService : IAutoJobLogService, ITransient
    {
        private readonly IRepository<AutoJobLogEntity> _autoJobLogDB;

        public AutoJobLogService(IRepository<AutoJobLogEntity> autoJobLogDB)
        {
            _autoJobLogDB = autoJobLogDB;
        }

        #region 获取数据

        /// <summary>
        /// 带条件查询所有
        /// </summary>
        public async Task<List<AutoJobLogEntity>> GetList(AutoJobLogListParam param)
        {
            IQueryable<AutoJobLogEntity> query = ListFilter(param);
            var data = await query.ToListAsync();
            return data;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        public async Task<List<AutoJobLogEntity>> GetPageList(AutoJobLogListParam param, Pagination pagination)
        {
            IQueryable<AutoJobLogEntity> query = ListFilter(param);
            var data = await query.OrderByDescending(a => a.Id).ToPagedListAsync(pagination.PageIndex, pagination.PageSize);

            // 分页参数赋值
            pagination.TotalCount = data.TotalCount;
            return data.Items.ToList();
        }

        /// <summary>
        /// 根据ID获取对象
        /// </summary>
        public async Task<AutoJobLogEntity> GetEntity(long id)
        {
            var data = await _autoJobLogDB.AsQueryable(p => p.Id == id).FirstOrDefaultAsync();
            return data;
        }

        /// <summary>
        /// 查询多个ID主键数据
        /// </summary>
        public async Task<List<AutoJobLogEntity>> GetListByIds(string ids)
        {
            if (ids.IsNullOrEmpty())
                throw new BusinessException("参数不合法！");

            var idArr = TextHelper.SplitToArray<long>(ids, ',').ToList();
            var data = await _autoJobLogDB.AsQueryable(a => idArr.Contains(a.Id.GetValueOrDefault())).ToListAsync();

            return data;
        }

        #endregion

        #region 提交数据

        public async Task SaveForm(AutoJobLogEntity entity)
        {
            if (entity.Id.IsNullOrZero())
            {
                // 默认赋值
                entity.Id = IdGeneratorHelper.Instance.GetId();
                await _autoJobLogDB.InsertNowAsync(entity);
            }
            else
            {
                await _autoJobLogDB.UpdateNowAsync(entity, true);
            }
        }

        public async Task DeleteForm(string ids)
        {
            if (string.IsNullOrWhiteSpace(ids))
                throw new BusinessException("参数不合法！");

            var _ids = ids.Split(",");
            await _autoJobLogDB.BatchFakeDeleteAsync(_ids);
        }

        public async Task DeleteFormByJobName(string jobName)
        {
            if (string.IsNullOrWhiteSpace(jobName))
                throw new BusinessException("参数不合法！");

            string sql = "Delete From SysAutoJobLog Where JobName =@jobName";
            await _autoJobLogDB.SqlNonQueryAsync(sql, new { jobName = jobName });
        }

        #endregion

        #region 私有方法
        private IQueryable<AutoJobLogEntity> ListFilter(AutoJobLogListParam param)
        {
            IQueryable<AutoJobLogEntity> query = _autoJobLogDB.AsQueryable();
            if (param != null)
            {
                // 任务组名称
                if (!string.IsNullOrEmpty(param.JobGroupName))
                    query = query.Where(p => p.JobGroupName.Contains(param.JobGroupName));
                // 任务名称
                if (!string.IsNullOrEmpty(param.JobName))
                    query = query.Where(p => p.JobName == param.JobName);
                // 执行状态(0失败 1成功)
                if (param.LogStatus.HasValue)
                    query = query.Where(p => p.LogStatus == param.LogStatus);

            }

            return query;
        }

        #endregion
    }
}